package lambdasinaction.chap5;

import lambdasinaction.chap4.Dish;

import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;
import static lambdasinaction.chap4.Dish.menu;

/**
 * 项目名称: java8
 * 包 名 称: lambdasinaction.chap5
 * 类 名 称: Test
 * 类 描 述: TODO
 * 创建时间: 2020/9/23 10:44 下午
 * 创 建 人: Justice
 */
public class Test {
    public static void main(String[] args) {
        // Stream.iterate
//        Stream.iterate(0, n -> n + 2)
//                .limit(10)
//                .forEach(System.out::println);

//        Stream.iterate(new int[]{0, 1}, t -> new int[]{t[1], t[0] + t[1]})
//                .limit(10)
//                .map(t -> t[0])
//                .forEach(System.out::println);


        // Skipping elements
//        List<Dish> dishesSkip2 =
//                menu.stream()
//                        .filter(d -> d.getCalories() > 300)
//                        .skip(2)
//                        .collect(toList());
//
//        dishesSkip2.forEach(System.out::println);

//        List<String> dishNames = menu.stream()
//                .map(Dish::getName)
//                .collect(toList());
//        System.out.println(dishNames);
//        List<String> words = Arrays.asList("Hello", "World");
//        List<Integer> wordLengths = words.stream()
//                .map(String::length)
//                .collect(toList());
//        System.out.println(wordLengths);
//
//        words.stream()
//                .flatMap((String line) -> Arrays.stream(line.split("")))
//                .distinct()
//                .forEach(System.out::println);
//        System.out.println("***************************************");
//        List<Integer> numbers1 = Arrays.asList(1, 2, 3, 4, 5);
//        List<Integer> numbers2 = Arrays.asList(6, 7, 8);
//        List<int[]> pairs =
//                numbers1.stream()
//                        .flatMap((Integer i) -> numbers2.stream()
//                                .map((Integer j) -> new int[]{i, j})
//                        )
//                        .filter(pair -> (pair[0] + pair[1]) % 3 == 0)
//                        .collect(toList());
//        pairs.forEach(pair -> System.out.println("(" + pair[0] + ", " + pair[1] + ")"));


        Stream<int[]> pythagoreanTriples =
                IntStream.rangeClosed(1, 100).boxed()
                        .flatMap(a -> IntStream.rangeClosed(a, 100)
                                .filter(b -> Math.sqrt(a * a + b * b) % 1 == 0).boxed()
                                .map(b -> new int[]{a, b, (int) Math.sqrt(a * a + b * b)}));

        pythagoreanTriples.forEach(t -> System.out.println(t[0] + ", " + t[1] + ", " + t[2]));
    }
}
